﻿using System;
using System.IO;

/// <summary>
/// 文件转换助手
/// </summary>
public static class FileConvertHelper
{
	/// <summary>
	/// 文件到字节集
	/// </summary>
	/// <returns>数据</returns>
	/// <param name="path">路径</param>
	public static byte[] FileToBytes(string path)
	{
		if(!File.Exists(path))
		{
			return new byte[0];
		}

		FileInfo fileInfo = new FileInfo(path);
		byte[] buff = new byte[fileInfo.Length];
		
		FileStream fileStream = fileInfo.OpenRead();
		fileStream.Read(buff, 0, Convert.ToInt32(fileStream.Length));
		fileStream.Close();
		
		return buff;
	}
	
	/// <summary>
	/// 字节集到文件
	/// </summary>
	/// <param name="buff">数据</param>
	/// <param name="path">路径</param>
	public static void BytesToFile(byte[] buff, string path)
	{
		if (File.Exists(path))
		{
			File.Delete(path);
		}
		
		FileStream fileStream = new FileStream(path, FileMode.CreateNew);
		BinaryWriter binaryWriter = new BinaryWriter(fileStream);
		binaryWriter.Write(buff, 0, buff.Length);
		binaryWriter.Close();
		fileStream.Close();
	}
}
